home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / FORTH / FORTHMAC / OLD / DOCS / !Forthmacs.docs.ascii.armassem < prev    next >
Encoding:
Text File  |  1996-06-15  |  22.5 KB  |  658 lines

  1.  
  2. ARM Assembler
  3. *************
  4.  
  5.  
  6. Using the assembler
  7. ===================
  8.  
  9. Coding in ARM assembler is very straightforward.  If you have used an 
  10. ARM Assembler before, you will already know the instructions.  
  11. Otherwise I recommend 
  12.  
  13. 'Acorn Risc Machine Family Data Manual, Prentice Hall, ISBN 0-13-781618-9' 
  14.  
  15. for further information.  It tells you everything about the ARM cpu 
  16. you should know and covers the whole instruction set plus lots of 
  17. hardware details.  In fact it was my only source of information at 
  18. hand when starting this RISC OS Forthmacs port.  
  19.  
  20. This documentation is by far not complete, but it covers most aspects.  
  21. If you are writing code, just have a look at some kernel sources and 
  22. see how it works.  Whenever you are not sure about the produced code, 
  23. have a look at it by 
  24.     code demo ...... c;
  25.     see demo
  26. and you have the code just in front of you.  
  27.  
  28. Also there is a chapter "Assembler Tutorial".  
  29.  
  30. As most Forth assemblers are, this assembler is really just a 
  31. vocabulary which contains the words for assembling ARM code.  It is 
  32. "activated" by adding the assembler vocabulary to the search order.  
  33. There are also some common ways to control assembly which do more than 
  34. just put the assembler vocabulary in the search order.  It also uses a 
  35. 'data first - operand last' syntax as Forth generally does.  
  36.  
  37. Lets now have a look at some kernel source and see what the syntax 
  38. looks like in the forth assembler syntax and in the original Acorn 
  39. Syntax ( displayed by the disassembler utility).  
  40.  
  41. code count      (s adr -- adr1 cnt ) 
  42.         r0      top     mov
  43.         top     r0 byte )+ ldr
  44.         r0      sp      push c;
  45. see count
  46. code count 
  47.  (   a148 )  mov     r0,r10
  48.  (   a14c )  ldrb    r10,[r0],#1
  49.  (   a150 )  str     r0,[r13,#-4]!
  50.  (   a154 )  ldr     pc,[r8],#4
  51.  
  52.  
  53. General syntax
  54. ==============
  55.  
  56. All instructions follow the general syntax: 
  57.  
  58.         ARM:        opcode  r-dest r-n operand
  59.         Forth:      r-dest r-nsrc operand modifiers  condition op-code
  60.  
  61.  
  62. The brackets and commas in the original assembler source are replaced 
  63. by spaces, 'addressing mode indicators' and macros.  The 
  64.  
  65. ia [r0],#4 will be )+ , indicating a postincrement by 1 or 4 according 
  66. to byte/word access.  
  67.  
  68. push is a macro meaning 
  69.     -( str.
  70.  
  71. c; at the end assembles a next instruction 
  72.     ldr     pc,[r8],#4
  73.     pc  ip )+  ldr
  74. and quits assembling.  
  75.  
  76. The operands ( registers or numbers ) must appear in the correct order 
  77. followed by modifiers.  
  78.  
  79.  
  80. Conditions
  81. ==========
  82.  
  83. All instructions can be conditionally executed on ARM cpus.  All 
  84. condition codes are implemented, they should be preferably written 
  85. just before the opcode itself.  You don't have to write down the AL 
  86. condition, it is the default.  
  87.  
  88. Note: According to ARM standards, NV is NOT implemented and should 
  89. never be used because of future instruction set extensions.  
  90.  
  91. Condition codes available : 
  92. EQ NE CS CC MI PL VS VC HI LS GE LT GT LE AL 
  93.  
  94.  
  95. Shifts
  96. ======
  97.  
  98. There a numerous shifts for operators available, 
  99.  
  100. ASL #ASL LSL #LSL LSR #LSR ASR #ASR ROR #ROR RRX , 
  101.  
  102. all shift operator leaded by a # mean count of shift specified by a 
  103. number, otherwise by a register.  
  104.  
  105. This assembler is clever enough to find out shifted immediates itself, 
  106. so you don't have to worry about lines like 
  107.     top th f0 #  td 24 #lsl mov
  108. just write 
  109.     top th f0000000 # mov
  110. instead.  
  111.  
  112.  
  113. Register usage
  114. ==============
  115.  
  116. Registers R0 - R6 are available for use within code definitions.  
  117. Don't try to use them for permanent storage, because they are used by 
  118. many code words with no attempt to preserve the previous contents.  
  119.  
  120.     r7      floating stack pointer  fsp
  121.     r8      instruction pointer     ip
  122.     r9      user area pointer       up
  123.     r10     top-of-stack register   top
  124.     r11     return stack pointer    rp
  125.     r12     RISC OS frame pointer    fp never touch this
  126.     r13     stack pointer           sp
  127.     r14     link register           lk
  128.     r15     pc + status + flags     pc
  129. Note: In future CPU Versions, the internal structure of the PC 
  130. register will be different, it seems to be better, to imagine PC and 
  131. status register as two registers.  The hardware-errors and the 
  132. .REGISTERS instruction know about this already.  
  133.  
  134.  
  135. Structured programming
  136. ======================
  137.  
  138. This assembler supports structured programming not by using labels but 
  139. common forth-like structures instead.  The structures do not have to 
  140. fit on one line, and they may be nested to any level.  The range of 
  141. the branches assembled by these structures is not restricted.  
  142.  
  143. Implemented structures are: 
  144.     set the flags                   \ produce the condition
  145.     condition if ...                \ if condition is met do this
  146.               else ...              \ otherwise this
  147.               then
  148.     
  149.     
  150.     
  151.     begin ....
  152.           set the flags             \ produce the condition
  153.     condition     while ...         \ do this when condition met
  154.           ( you may set the flags )
  155.     ( condition ) repeat            \ the repeat is normally always done
  156.                                     \ but you may also test for another
  157.                                     \ condition.
  158.     
  159.     
  160.     begin ...
  161.           set the flags             \ produce the condition
  162.     condition until                 \ leave the loop when condition is met
  163.     
  164.     
  165.     
  166.     begin ... again                 \ loop until whatever may happen
  167.  
  168.  
  169. Porting
  170. =======
  171.  
  172. The ARM assembler can be used also by other Forth systems, all 
  173. hardware specific parts are written portable and can be changed in 
  174. case of problems very easily.  So a 68k-Forthmacs can metacompile ARM 
  175. code by this assembler without any change.  In fact, the very first 
  176. metacompilation of this RISC OS Forthmacs took place on an ATARI-ST 
  177. having 1MB Ram and a 720k disk.  
  178.  
  179.  
  180. Byte-sex
  181. ========
  182.  
  183. Both byte-sexes can be produced by this assembler, this allows 
  184. portable assembler code for all ARM CPUs.  LITTLE-ENDIAN and 
  185. BIG-ENDIAN do the switch.  
  186.  
  187.  
  188. ARM2/3/6
  189. ========
  190.  
  191. The assembler takes care of some cpu dependent restrictions, ARM2 
  192. disallows the more advanced instructions, ARM3 allows them.  
  193.  
  194.  
  195. Forth Virtual Machine Considerations
  196. ====================================
  197.  
  198. The Forth parameter stack is implemented with r13, but the name SP 
  199. should be used instead of r13, in case the virtual machine 
  200. implementation should change.  
  201.  
  202. The return stack is implemented with r11, and the name RP should be 
  203. used to refer to it.  
  204.  
  205. The base address of the user area ( the user pointer) is r9 but should 
  206. be referred to as UP. User variable number 124 (for instance) may be 
  207. accessed with the 
  208.     up td 124 d)
  209. addressing mode.  There is a macro 'USER which will assemble this 
  210. addressing mode for you.  
  211.  
  212. The interpreter pointer IP is r8.  The interpreter is 
  213. post-incrementing, so when a code definition is being executed, IP 
  214. points to the token after the one being executed.  A "token" is the 
  215. number that is compiled into the dictionary for each Forth word in a 
  216. definitions.  For RISC OS Forthmacs, a token is a 32-bit absolute 
  217. address.  
  218.  
  219.  
  220. Assembler Glossary 
  221. ===================
  222.  
  223.  
  224. ________________________________________________________________________
  225. PC                  ( -- n )                                
  226. portable name for the PC register 
  227.  
  228.  
  229. ________________________________________________________________________
  230. SP                  ( -- n )                                
  231. portable name for the stack pointer 
  232.  
  233.  
  234. ________________________________________________________________________
  235. FSP                 ( -- n )                                
  236. portable name for the floating stack pointer 
  237.  
  238.  
  239. ________________________________________________________________________
  240. UP                  ( -- n )                                
  241. portable name for the user pointer 
  242.  
  243.  
  244. ________________________________________________________________________
  245. IP                  ( -- n )                                
  246. portable name for the instruction pointer 
  247.  
  248.  
  249. ________________________________________________________________________
  250. RP                  ( -- n )                                
  251. portable name for the return stack pointer 
  252.  
  253.  
  254. ________________________________________________________________________
  255. TOP                 ( -- n )                                
  256. portable name for the top of stack register 
  257.  
  258.  
  259. ________________________________________________________________________
  260. 'USER               ( --  )        'name'                   
  261. Executed in the form: 
  262.          top   'user <name>   ldr
  263. <name> is the name of a User variable.  Assembles the appropriate 
  264. addressing mode for accessing that User variable.  
  265.  
  266. In RISC OS Forthmacs, the addressing mode for User variables is 
  267.                  up #n d)
  268. where #n is the offset of that variable within the User area.  
  269.  
  270.  
  271. ________________________________________________________________________
  272. ;CODE               ( --  )                  C,I            
  273. semi-colon-code
  274.                     ( --  )
  275. Used in the form: 
  276.                  : <name>  ... create ... ;code ... c; (or end-code)
  277. Stops compilation, terminates the defining word <name>, executes 
  278. ASSEMBLER, and does DO-ENTERCODE. 
  279.  
  280. When <name> is later executed in the form: 
  281.                  <name> <new-name>
  282. to define the word <new-name>, the later execution of <new-name> will 
  283. cause the machine code sequence following the ;CODE to be executed.  
  284.  
  285. This is analogous to DOES>, except that the behavior of the defined 
  286. words <word-name> is specified in assembly language instead of 
  287. high-level Forth.  
  288.  
  289. ;CODE calls DO-ENTERCODE, this is implementation specific and 
  290. assembles the code needed to start the assembler code with the body of 
  291. the defined word in TOP 
  292.          top     sp      push
  293.          top     lk      th fc000003 # bic
  294.  
  295. Note for specialists: You may do 
  296.     ;code
  297.       -8 ass-allot
  298.       ...
  299. and handle the link register and stack on your own which can be 
  300. somewhat faster.  
  301.  
  302. See: CODE DOES> 
  303.  
  304.  
  305. ________________________________________________________________________
  306. ADR                 ( rx addr --  )                         
  307. Assembler macro with the following effect: 
  308.  
  309. addr is moved to register rx.  Within short distances this is achieved 
  310. by a PCR instruction, otherwise it's more complicated.  
  311.  
  312. Note: The address will be relocated correctly! 
  313.  
  314.  
  315. ________________________________________________________________________
  316. ALIGNING?           ( -- addr  )                            
  317. variable holding flag, true means assembler does aligning on its own.  
  318. Implemented for CPU independent metacompiling.  
  319.  
  320.  
  321. ________________________________________________________________________
  322. ALU-INSTRUCTIONS    ( r-dest r-op1 op2{r-op2|imm} --  )     
  323. Available instructions with this syntax: 
  324.  
  325. AND EOR SUB RSB ADD ADC SBC RSC TST TEQ CMP CMN ORR BIC  
  326.  
  327. These instructions all have two data-inputs to the alu, the register 
  328. r-op1 and the operand op2.  This can be another register or an 8-bit 
  329. immediate.  
  330.  
  331. The register r-op2 can be "shifted" in any way specified by a shift 
  332. specifier, either a 5-bit integer or another register plus the shifted 
  333. register.  The immediate operand can be rotated right by 
  334. 2*(4-bit-integer).  
  335.  
  336. If you give "large" literals as arguments, the assembler will generate 
  337. the correct shifts itself.  
  338.  
  339. The # modifier declares an immediate operand as in: \ top r0 3 # add 
  340.  
  341. The S modifier will set the flags according to the result, the 
  342. instruction will be ADDS instead of ADD .  
  343.  
  344. MOV and MVN are somewhat different, the operand r-op1 isn't needed.  
  345. Also, both can handle "big" immediates themselves, 
  346.          top th 12345678 # mov
  347. won't be a problem, MOV assembles all instructions needed.  
  348.  
  349. CMP and (Fcmn) can both handle negative immediate operandes, they try 
  350. to find out which operand is possible.  
  351.  
  352.  
  353. ________________________________________________________________________
  354. ASS-ALLOT           ( n --  )                deferred       
  355. Allocates n bytes in the dictionary.  The address of the next 
  356. available dictionary location is adjusted accordingly.  
  357.  
  358. default ALLOT, implemented for ( cpu independent ) metacompiling.  
  359.  
  360.  
  361. ________________________________________________________________________
  362. ASSEMBLER           ( --  )                                 
  363. Execution replaces the first vocabulary in the search order with the 
  364. ASSEMBLER vocabulary, making all the assembler words accessible.  
  365.  
  366.  
  367. ________________________________________________________________________
  368. BIG-ENDIAN          ( -- )                                  
  369. Switches assembler to big-endian target code 
  370.  
  371.  
  372. ________________________________________________________________________
  373. BRANCH              ( addr --  )                            
  374. Assembles a branch instruction to here.  Can be modified by DOLINK and 
  375. all condition codes.  
  376.  
  377.  
  378. ________________________________________________________________________
  379. BYTE                ( -- )                                  
  380. modifier for the assembler, memory accesses mean byte wide access 
  381.  
  382.  
  383. ________________________________________________________________________
  384. CODE                ( --  )        'name'    M              
  385. A defining word executed in the form: 
  386.                  code <name> ... end-code or c;
  387. Creates a dictionary entry for <name> to be defined by a following 
  388. sequence of assembly language words.  Words thus defined are called 
  389. code definitions or primitives.  Executes ASSEMBLER and sets the 
  390. opcode defaults .  
  391.  
  392. This is the most common way to begin assembly.  
  393.  
  394.  
  395. See: END-CODE C; 
  396.  
  397.  
  398. ________________________________________________________________________
  399. CODE!               ( n addr --  )           Deferred       
  400. Stores a 32-bit word into the code at addr.  
  401.  
  402. This word is deferred so that the metacompiler may change it to 
  403. assemble code into the target dictionary rather than the resident 
  404. dictionary.  It also handles little/big endian target code.  
  405.  
  406.  
  407. ________________________________________________________________________
  408. CODE,               ( n --  )                Deferred       
  409. Places n in the dictionary at ( assemblers ) HERE and ASS-ALLOTs 
  410. enough space for a word.  
  411.  
  412. This word is deferred so that the metacompiler may change it to 
  413. assemble code into the target dictionary rather than the resident 
  414. dictionary.  It also handles little/big endian target code.  
  415.  
  416.  
  417. ________________________________________________________________________
  418. C;                  ( --  )                                 
  419. c-semi-colon
  420. Terminates the current code definition and allows its name to be found 
  421. in the dictionary.  
  422.  
  423. Sets the CONTEXT vocabulary to be same as the CURRENT vocabulary 
  424. (which removes the ASSEMBLER vocabulary from the search order, unless 
  425. you have explicitly done something funny to the search order while 
  426. assembling the code).  
  427.  
  428. Executes NEXT to assemble the "next" routine at then end of the code 
  429. word word being defined.  The "next" routine causes the Forth 
  430. interpreter to continue execution with the next word.  
  431.  
  432.  
  433. This is the most common way to end assembly, calls END-CODE. 
  434.  
  435.  
  436. ________________________________________________________________________
  437. CONDITIONS          ( --  )                                 
  438. All instruction are executed only if the correct condition is met, the 
  439. assemblers default is AL (always), but these are also available: 
  440.  
  441. EQ NE CS CC MI PL VS VC HI LS GE LT GT LE AL 
  442.  
  443.  
  444. ________________________________________________________________________
  445. DECR                ( reg n# --  )                          
  446. Macro, n# will be subtracted from reg.  
  447.  
  448.  
  449. ________________________________________________________________________
  450. DOLINK              ( --  )                                 
  451. modifier for BRANCH instruction, the current pc will be saved to the 
  452. link register.  
  453.  
  454.  
  455. ________________________________________________________________________
  456. END-CODE            ( --  )                                 
  457. Terminates a code definition and allows the <name> of the 
  458. corresponding code definition to be found in the dictionary.  
  459.  
  460. The CONTEXT vocabulary is set to the same as the CURRENT vocabulary 
  461. (which removes the ASSEMBLER vocabulary from the search order, unless 
  462. you have explicitly done something funny to the search order while 
  463. assembling the code).  
  464.  
  465. The NEXT routine is not automatically added to the end of the code 
  466. definition.  Usually you want NEXT to be at the end of the definition, 
  467. but sometimes the last thing in the definition is a branch to 
  468. somewhere else, so the NEXT at the end is not needed.  
  469.  
  470.  
  471. See: C; 
  472.  
  473.  
  474. ________________________________________________________________________
  475. ENTERCODE           ( --  )                                 
  476. Starts assembling after stack checking, setting the assembler defaults 
  477. and switching to ASSEMBLER. 
  478.  
  479.  
  480. ________________________________________________________________________
  481. GET-LINK            ( -- reg --  )                          
  482. Assembler macro, equivalent for: 
  483.     lk fc000003 # bic
  484. this is useful to get the address after a branch instruction.  
  485.     xxxxx dolink branch  ---+
  486.       A) data ...           |
  487.                             |
  488.                             |
  489.       B) top get-link  <----+
  490. So after branching to B), TOP will be set to A) 
  491.  
  492.  
  493. ________________________________________________________________________
  494. INCR                ( reg n# --  )                          
  495. Macro, n# will be added to reg.  
  496.  
  497.  
  498. ________________________________________________________________________
  499. LABEL               ( --  )        'name'    F83            
  500. A defining word used in the form: 
  501.     label <name> ... end-code
  502.     label <name> ... c;
  503. Creates a dictionary entry for <name> consisting of a following 
  504. sequence of assembly language words.  When <name> is later executed, 
  505. the address of the first word of the assembly language sequence is 
  506. left on the stack.  
  507.  
  508.  
  509. See: END-CODE 
  510.  
  511.  
  512. ________________________________________________________________________
  513. LDM                 ( rx1 rx2 .. rxn  n#  r-adr --  )       
  514. Load multiple registers from the address pointed to by r-adr, an 
  515. addressing modes must be defined.  
  516.  
  517. The register list is given by all register names (don't name a 
  518. register twice) and the number of registers.  
  519.      r0 r1 r2 r3 4   sp ia! ldm
  520. This loads registers r0-r3 from the stack and sets the stack pointer 
  521. to the next stack entry.  
  522.  
  523.  
  524. See: LDR STM 
  525.  
  526.  
  527. ________________________________________________________________________
  528. LDR                 ( r-data r-adr operand2 --  )           
  529. r-data is read from memory, the default is word (32-bit) wide, but the 
  530. modifier BYTE sets this byte-wide access.  
  531.  
  532. The address is calculated using r-adr and the operand2.  It can be 
  533. another register (the shift specified as usual by a 5-bit literal and 
  534. a shift type) or a 12-bit immediate offset.  
  535.  
  536. operand2 can be added to or subtracted from r-adr according to the 
  537. addressing mode defined by two letters.  The first tells whether 
  538. (i)ncreasing or (d)decreasing should be used, the second whether the 
  539. in/decreasing takes place (b)efore or (a)fter the memory access.  A 
  540. "!" at the end tells "write-back" will take place.  So these modes are 
  541. possible 
  542.          da  ia  db  ib    \ decrease/increase after/before
  543.          da  ia! db! ib!   \ as above plus write-back
  544.  
  545.  
  546. Some macros make live a bit more easy, they are somewhat 68k alike, 
  547. and must follow a BYTE modifier because an offset will be calculated 
  548. by the assembler itself.  
  549.  
  550.     : )      0 #   ib ;
  551.     : )+     @increment  ia ;
  552.     : )-     @increment  da ;
  553.     : -(     @increment  db! ;
  554.     : +(     @increment  ib! ;
  555.     
  556.     : d)     dup abs # offset?  swap 0<  if db  else ib  then ;
  557.     : d)!    dup abs # offset?  swap 0<  if db! else ib! then ;
  558.     : push   -( str ;
  559.     : pop    )+ ldr ;
  560. Examples: 
  561.       top  r6 byte )+ ldr
  562.       top  up 8 d)    ldr
  563.  
  564.  
  565. See: STR 
  566.  
  567.  
  568. ________________________________________________________________________
  569. LITTLE-ENDIAN       ( -- )                                  
  570. Switches assembler to little-endian target code 
  571.  
  572.  
  573. ________________________________________________________________________
  574. MLA                 ( r-dest r-op1 r-op2 )                  
  575. Assembles a multiply-and-accumulate instruction.  
  576.  
  577.  
  578. ________________________________________________________________________
  579. MUL                 ( r-dest r-op1 r-op2 )                  
  580. Assembles a multiply instruction.  
  581.  
  582.  
  583. ________________________________________________________________________
  584. NEXT                ( --  )                                 
  585. Assembler macro which assembles the NEXT routine, which is the Forth 
  586. address interpreter.  
  587.  
  588. In RISC OS Forthmacs this is one single instruction.  
  589.          pc  ip  )+  ldr
  590.  
  591.  
  592. ________________________________________________________________________
  593. NOP                 ( --  )                                 
  594. Assembler macro, equivalent to 
  595.     r0 r0 mov
  596.  
  597.  
  598. ________________________________________________________________________
  599. PCR                 ( addr -- pc offset  )                  
  600. Assembler macro, expects an address on the stack and calculates its 
  601. address offset from PC. The addressing mode is also set.  
  602.  
  603.  
  604. ________________________________________________________________________
  605. RETURN              ( --  )                                 
  606. macro for 
  607.     pc  lk  mov
  608.  
  609.  
  610. ________________________________________________________________________
  611. S                   ( --  )                                 
  612. modifier, the instruction will set the flags according to the result.  
  613. default for tst, teq tstp teqp cmp cmn cmpp cmnp.  
  614.  
  615.  
  616. ________________________________________________________________________
  617. STM                 ( rx1 rx2 .. rxn  n#  r-adr --  )       
  618. Store multiple registers to the address pointed to by r-adr, an 
  619. addressing modes must be defined.  
  620.  
  621.  
  622. See: LDM for more details.  
  623.  
  624.  
  625. ________________________________________________________________________
  626. STR                 ( r-data r-adr operand2 --  )           
  627. r-data is stored to memory, the default is word (32-bit) wide, but the 
  628. modifier BYTE sets this byte-wide access.  
  629.  
  630.  
  631. See: LDR 
  632.  
  633.  
  634. ________________________________________________________________________
  635. SWI                 ( swi# --  )                            
  636. assembles a swi instruction, the number is swi#.  
  637.  
  638.  
  639. ________________________________________________________________________
  640. SWIX                ( swi# --  )                            
  641. assembles a swix instruction, the number is swi#.  
  642.  
  643.  
  644. ________________________________________________________________________
  645. SWP                 ( r-dest r-base r-source --  )          
  646. assembles a swp instruction if Arm3-code is allowed by ARM3 
  647.  
  648.  
  649. ________________________________________________________________________
  650. T                   ( --  )                                 
  651. modifier, force -T pin.  
  652.  
  653.  
  654. ________________________________________________________________________
  655. ^                   ( --  )                                 
  656. modifier, force access to user-mode registers.  
  657.  
  658.